package com.koron.css2.clientCenter.impl;

import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.bean.UserCloseBean;
import com.koron.css2.clientCenter.bean.UserCloseDetailBean;
import com.koron.css2.clientCenter.mapper.ChangeUserMapper;
import com.koron.css2.clientCenter.mapper.CloseUserMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.bean.MsChgPlanBean;
import com.koron.css2.meterService.bean.MsChgPlanDetailBean;
import com.koron.css2.meterService.bean.MsOutInBean;
import com.koron.css2.meterService.mapper.MsChgPlanDetailMapper;
import com.koron.css2.meterService.mapper.MsChgPlanMapper;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.meterService.mapper.MsOutInMapper;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.workflow.ProcessInterface;
import com.koron.util.Constant;
import com.koron.util.DateUtils;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 销户审批处理
 *
 * @author 甄玉晴
 *
 */
public class CloseUserWorkFlow implements ProcessInterface {

	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
		MessageBean<?> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", Void.class);
		try {
			// 审批后执行
			if("approval".equalsIgnoreCase(fromNodeCode) && 32 == operation) {
				// 获取销户的单据信息
				CloseUserMapper mapper = factory.getMapper(CloseUserMapper.class);
				UserCloseBean bean = mapper.getUserCloseInfoById(billId);
				// 修改用户信息
				UserInfoMapper userMapper = factory.getMapper(UserInfoMapper.class);
//				UserBean user=userMapper.getUserInfoByUserNo(bean.getUserNo(),UserBean.USER);
				UserBean user= new UserBean();
				user.setCancelDate(bean.getCancelDate());
				user.setStatus(0+"");
				user.setSplitMeterWay(1+"");
				user.setUpdateInfo(userInfo);
				//获取本单据所有销户信息
				UserCloseDetailBean userCloseDetailBean = new UserCloseDetailBean();
				userCloseDetailBean.setUserCloseId(bean.getId());
				List<UserCloseDetailBean> list = mapper.userCloseDetailList(userCloseDetailBean);
				if (list.size() != 0){
					//销户操作，修改用户状态
					for (UserCloseDetailBean userCloseDetailBean1:list){
						user.setUserNo(userCloseDetailBean1.getUserNo());
						userMapper.closeUser(user);
					}
				}
				//更新变更事项到正式表
				ChangeUserMapper cmapper = factory.getMapper(ChangeUserMapper.class);
				cmapper.userChangeItemfromTemp(billId);


				//生成拆表单据
				/*if(bean.getRemoveFlag()!=null&&bean.getRemoveFlag()==1) {
					addMsChgPlan(factory,user,bean,userInfo);
				}*/
				// 5、旧表状态改为停用
				MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);
				for (UserCloseDetailBean userCloseDetailBean1:list){
					String meterNo = msInfoMapper.geMeterNoByUserNo(userCloseDetailBean1.getUserNo());
					if("1".equals(userCloseDetailBean1.getRemoveFlag())) {
						//是拆表则表状态报废
						msInfoMapper.updateMsInfoStatus(meterNo,4);
					}else {
						//置为在库
						msInfoMapper.updateMsInfoStatus(meterNo,1);
					}
					msInfoMapper.updateMsInfoUserNo(meterNo, ""); // 将旧表关联的户号置为空
					// 4、添加水表操作记录表
					MsOutInBean msOutInfo = new MsOutInBean();
					msOutInfo.setMeterNo(meterNo);
					msOutInfo.setOperateComments( "拆表操作");
					msOutInfo.setOperateTime(DateUtils.parseDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
					msOutInfo.setOperateStaff(userInfo.getUserInfo().getName());
					msOutInfo.setOperateStaffId(userInfo.getUserInfo().getAcount());
					msOutInfo.setOperateType(MsOutInBean.CHANGE);
					msOutInfo.setCreateInfo(userInfo);
					MsOutInMapper msOutInMapper = factory.getMapper(MsOutInMapper.class);
					int iCount = msOutInMapper.addMsOutIn(msOutInfo);
					if(iCount > 0) {
						logger.info("插入水表操作记录成功 水表编号：{}",bean.getMeterNo());
					}
				}
			}
		} catch (Exception e) {
			factory.close(false);
			logger.error("销户审批后续操作失败", e);
			info = MessageBean.create(Constant.MESSAGE_INT_FAIL, "销户审批后续操作失败", Void.class);
		}
		return info;
	}
	/**
	 * 生成拆表单据
	 * @param factory
	 * @param user
	 * @param userInfo
	 */
	public void addMsChgPlan(SessionFactory factory,UserBean user,UserCloseBean close,UserInfoBean userInfo) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		//获取拆表信息
		MsChgPlanBean bean=new MsChgPlanBean();
		try {
			bean.setBillDate(simpleDateFormat.parse(close.getBillDate()));
			bean.setBillNo(Tools.getBillNo(factory, "MS_CHG_PLAN"));
			bean.setReceiptType(2);
			bean.setReplaceType(Integer.valueOf(user.getSplitMeterWay()));
			bean.setMeterAmount(1);
			bean.setBackfillAmount(0);
			bean.setCreateInfo(userInfo);
			MsChgPlanMapper msMapper=factory.getMapper(MsChgPlanMapper.class);
			//添加单据信息
			msMapper.addMsChgPlan(bean);
			//添加明细表
			MsChgPlanDetailBean detail=new MsChgPlanDetailBean();
			detail.setReceiptId(bean.getId());
			detail.setMargin(0);
			detail.setUserNo(user.getUserNo());
			detail.setOldMeterNo(user.getMeterNo());
			detail.setUpperNum(user.getLastReadingNum());
			detail.setCreateInfo(userInfo);
			MsChgPlanDetailMapper detailMapper=factory.getMapper(MsChgPlanDetailMapper.class);
			detailMapper.addMsChgPlanDetail(detail);
		} catch (Exception e) {
			logger.error("销户审批后续操作增加拆表单据失败", e);
			e.printStackTrace();
		}
	}
	@Override
	public MessageBean<?> before(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
		return exec(factory, userInfo, billId, fromNodeCode, operation);
	}

	@Override
	public MessageBean<?> after(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, String toNodeCode) {
		return null;
	}


}
